package unicall.em.controllers;


import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import unicall.em.dtos.MediaDto;
import unicall.em.dtos.SkillDto;
import unicall.em.models.Skill;
import unicall.em.models.vo.SkillQuery;
import unicall.em.services.MediaService;
import unicall.em.services.SkillService;
import unicall.em.utils.Constant;

@Controller
@RequestMapping(value = "/skill")
public class Skills {

    @Autowired
    SkillService skillService;

    @Autowired
    MediaService mediaService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(@ModelAttribute("query") SkillQuery query,@RequestParam(required=false,value="pageNumber",defaultValue="0") int pageNumber) {
        ModelAndView mv = new ModelAndView();
        Page<Skill> page = skillService.findAll(query.toSpec(), new PageRequest(pageNumber,Constant.PAGE_SIZE));
        List<SkillDto> dataList = page.getContent().stream().map(e->e.toDto()).collect(Collectors.toList());
        mv.addObject("dataList",dataList);
        mv.addObject("page", page);
        mv.setViewName("/skills/index");
        return mv;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        List<MediaDto> medias = mediaService.queryAll().stream().map(e -> e.toDto())
                .collect(Collectors.toList());
        model.addAttribute("medias", medias);
        model.addAttribute("skill", new SkillDto());
        return "/skills/add";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(@Valid @ModelAttribute("skill") SkillDto skill, BindingResult result,Model model) {
        if (result.hasErrors()) {
            List<MediaDto> medias = mediaService.queryAll().stream().map(e -> e.toDto())
                    .collect(Collectors.toList());
            model.addAttribute("medias",medias);
            return "/skills/add";
        } else {
            skillService.add(skill);
            return "redirect:/skill";
        }
    }

    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable("id") String id, Model model) {
        SkillDto skillDto = skillService.findById(id).toDto();
        model.addAttribute("skill", skillDto);
        List<MediaDto> medias = mediaService.queryAll().stream().map(e -> e.toDto())
                .collect(Collectors.toList());
        model.addAttribute("medias", medias);
        return "/skills/edit";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String update(@PathVariable("id") String id,
            @Valid @ModelAttribute("skill") SkillDto skill, BindingResult result,Model model) {
        if (result.hasErrors()) {
            List<MediaDto> medias = mediaService.queryAll().stream().map(e -> e.toDto())
                    .collect(Collectors.toList());
            model.addAttribute("medias",medias);
            return "/skills/edit";
        } else {
            skillService.update(skill);
            return "redirect:/skill";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String destroy(@PathVariable("id") String id) {
        skillService.destroy(id);
        return "redirect:/skill";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") String id, Model model) {
        Skill skill = skillService.findById(id);
        model.addAttribute("skill", skill.toDto());
        return "/skills/show";
    }

}
